import 'package:flutter/material.dart';
import 'package:aweb5/base/BaseWidget.dart';
import 'package:aweb5/bean/Entry.dart';
import 'package:aweb5/pages/Submit.dart';
class ShopCartDialog extends BaseWidget {
  List<Entry> _entry;
  ShopCartDialog(this._entry);
  @override
  State<ShopCartDialog> createState() => ShopCartDialogState();
}

class ShopCartDialogState extends BaseWidgetState<ShopCartDialog> {
  TextEditingController controller;
  int count=0;
  @override
  void initState() {
    super.initState();
    controller=TextEditingController();
  }

  @override
  Widget build(BuildContext context) {
    init(context);
    return Scaffold(
      body: Container(
        padding: EdgeInsets.only(top: 15),
        height: 445,
        child: Column(
          children: <Widget>[
            Row(
              children: <Widget>[
                SizedBox(
                  width: 10,
                ),
                FadeInImage.assetNetwork(
                    width: 100,
                    height: 100,
                    fit: BoxFit.fill,
                    placeholder: "assets/productimg.png",
                    image: ""),
                SizedBox(
                  width: 10,
                ),
                Expanded(
                  child: Container(
                      height: 100,
                      width: 100,
                      child: Stack(
                        children: <Widget>[
                          Positioned(
                              right: 0,
                              top: 0,
                              child: InkWell(
                                child: Container(
                                  width: 32,
                                  height: 32,
                                  padding: EdgeInsets.all(10),
                                  child: Image.asset(
                                    "assets/close.png",
                                    width: 12,
                                    height: 12,
                                  ),
                                ),
                                onTap: () {
                                  navigatorState.pop();
                                },
                              )),
                          Positioned(
                            child: Text(
                              "¥8",
                              style: TextStyle(
                                  color: Color(0xffff0000), fontSize: 18),
                            ),
                            left: 0,
                            top: 12,
                          ),
                          Positioned(
                            child: Container(
                              width: windowWidth - 100 - 30,
                              child: Text(
                                "每日更新花花牛双口味益生菌风味发酵乳花花牛双口味益生菌风餐酸奶180g ",
                                style: TextStyle(
                                    color: Color(0xff2d2d2d), fontSize: 13),
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                              ),
                            ),
                            left: 0,
                            top: 50,
                          )
                        ],
                      )),
                )
              ],
            ),
            SizedBox(
              height: 10,
            ),
            Divider(
              height: 3,
              color: Color(0xffe5e5e5),
              indent: 10,
              endIndent: 10,
            ),
            new Expanded(
                child: ListView.builder(
              padding: EdgeInsets.only(bottom: 50),
              shrinkWrap: true,
//            physics: NeverScrollableScrollPhysics(),
              itemBuilder: (BuildContext context, int index) {
                return expandList(index);
              },
              itemCount: widget._entry.length+1,

            )),
//            number()
          ],
        ),
      ),
      bottomSheet: GestureDetector(
        child: Container(
          alignment: Alignment.center,
          height: 49,
          width: windowWidth,
          color: Color(0xffff8900),
          child: Text(
            "确定",
            style: TextStyle(color: Colors.white, fontSize: 18),
          ),
        ),
        onTap: (){
          navigatorState.pop();
          navigatorState.push(MaterialPageRoute(builder: (c){return Submit();}));
        },
      ),
    );
  }

  Widget expandList(index) {
    if(index<widget._entry.length){
      return

        Column(
          children: <Widget>[
            CustomScrollView(
              physics: NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              slivers: <Widget>[
                SliverToBoxAdapter(
                  child:
                  Container(
                    margin: EdgeInsets.only(left: 10),
                    padding: EdgeInsets.only(top: 15),
                    height: 50,child: Text(
                    widget._entry[index].title,
                  ),)
                  ,
                ),

                SliverGrid(
                  delegate: SliverChildBuilderDelegate((
                      context,
                      index1,
                      ) {
                    return buildItem(index1, index);
                  }, childCount: widget._entry[index].children.length),
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 3,
                      mainAxisSpacing: 10,
                      childAspectRatio: getRatio()),
                ),
//        SliverList(
//          delegate: SliverChildBuilderDelegate((
//            context,
//            index1,
//          ) {
//            return buildItem(index1, index);
//          }, childCount: data[index].children.length),
//        ),
              ],
            ),
            Container(height:1,color: Color(0xffe9e9e9),margin: EdgeInsets.only(left: 10,right: 10,top: 10),)
          ],

        );
    }
    else{
      return number();
    }
    ;

  }

  Widget buildItem(index1, index) {
//    if(index1==widget._entry[index].children.length-1){
//
//    }else{
      return GestureDetector(
        child: Container(
          margin: EdgeInsets.only(left: 5, right: 5),
          alignment: Alignment.center,
          width: (windowWidth - 50) / 3,
          child: Text(widget._entry[index].children[index1].title),
          decoration: BoxDecoration(
              image: DecorationImage(
                  image: AssetImage(widget._entry[index].children[index1].clicked
                      ? "assets/rec_orange.png"
                      : "assets/rec_grey.png"),
                  fit: BoxFit.fill),
              borderRadius: BorderRadius.circular(10)),
        ),
        onTap: (){
          for (Entry e in widget._entry[index].children){
            e.clicked=false;
          }
          widget._entry[index].children[index1].clicked=true;
          setState(() {

          });
        },
      );
//    }

  }

  double getRatio() {
    double width = (windowWidth - 20 - 30 - 1) / 3;
    double height = 24;
    return width / height;
  }

  Widget number(){
    return Container(
//      height: 100,
//      width: windowWidth-20,
      margin: EdgeInsets.only(left: 10,right: 10,top: 20,bottom: 20),
      child: 
        
        
          Row(
            children: <Widget>[
              Text("数量",style: TextStyle(color: Color(0xff2d2d2d),fontSize: 14),),
              Expanded(child: Text("")),
              Column(
                children: <Widget>[
                  Row(
                    children: <Widget>[
                      InkWell(child:Image.asset("assets/jian_icon.png",width: 28,height: 28,) ,onTap: (){
                        if(controller.text.isNotEmpty&&count>0){
                          count--;
                        }
                        setState(() {
                          controller.text="${count}";
                        });
                      },)
                      ,
                      SizedBox(width: 10,),
                      Container(
                        width: 90,
                        height: 26,
                        foregroundDecoration: BoxDecoration(image: DecorationImage(image: AssetImage("assets/shuzikuang.png"))),
                        child: TextField(
                          textAlign: TextAlign.center,
                          controller: controller,
                          keyboardType: TextInputType.number,
                          onChanged: (v){
                            if(v.isNotEmpty)
                              count=int.parse(v);
                          },
                          decoration: InputDecoration(border: InputBorder.none,contentPadding: EdgeInsets.only(top: 2,left: 5,right: 2),),
                        ),),
                      SizedBox(width: 10,),
                      InkWell(child:Image.asset("assets/jia_icon.png",width: 28,height: 28,) ,onTap: (){
                        if(controller.text.isNotEmpty ){
                          count++;
                        }
                        setState(() {
                          controller.text="${count}";
                        });
                      },)
                    ],
                  ),
                  Text("点击输入框可手动输入数量",style: TextStyle(color: Color(0xfffe5000),fontSize: 11),)
                ],
              ),
              
             
            ],
          ),
          
          
          
       
     
    );
  }
}
